#set ff=unix
#!/bin/bash
if [ $# -ne 3 ]; then
  echo "ERROR: wrong number of arguments given."
  echo "Usage: `basename $0`   <mode1> <mode2> <mode3>"
  exit 0
fi

BNIC_MASK="255.255.255.0"
BNIC_GW="135.251.194.1"

#bond0 interface 0 config
BNIC0_ADDR="135.251.195.1"
BNIC0="bond0"
BNIC0_SLAVE=("em1" "em2");

#bond1 interface 1 config
BNIC1_ADDR="135.251.194.2"
BNIC1="bond1"
BNIC1_SLAVE=("em3" "em4");

#bond2 interface 2 config
BNIC2_ADDR="135.251.196.1"
BNIC2="bond2"
BNIC2_SLAVE=("em5" "em6");

FILENAME=interfaces
[ -e $FILENAME ] && rm $FILENAME
FILENAME2=modules
[ -e $FILENAME2 ] && rm $FILENAME2
echo "
		auto lo
                iface lo inet loopback
" >> $FILENAME

#obtain the length of each slave_array
length0=${#BNIC0_SLAVE[@]};
length1=${#BNIC1_SLAVE[@]};
length2=${#BNIC2_SLAVE[@]};

# write the first bond0 to interfaces
if [ $length0 -ne 0 ]; then
	for((i=0;i<$length0;i++));do
		echo "
		auto ${BNIC0_SLAVE[i]}
		iface ${BNIC0_SLAVE[i]} inet manual
		bond-master $BNIC0"  >>$FILENAME
	if [ $1 -eq 1 ]; then
		if [ $i -eq 0 ]; then
                echo "
		bond-primary ${BNIC0_SLAVE[i]}" >>$FILENAME
		fi
	fi
	done
	
	echo "
	        auto    $BNIC0
                iface   $BNIC0 inet static
                address $BNIC0_ADDR
                netmask $BNIC_MASK
                gateway $BNIC_GW
        " >>$FILENAME
  
  for((i=0;i<$length0;i++));do
	echo "
                up ifenslave $BNIC0 ${BNIC0_SLAVE[i]}
                down ifenslave $BNIC0 -d ${BNIC0_SLAVE[i]}
	"  >>$FILENAME
	done
	  
  echo "
                bond-mode $1
   	        bond-miimon 100
                bond-slaves none
  "   >>$FILENAME
	echo "
		alias $BNIC0 bonding
	        options $BNIC0 mode=$2 miimon=100 max_bonds=2 
	     " >>$FILENAME2

fi
 
#write the second bond1 to interfaces
if [ $length1 -ne 0 ]; then
        for((i=0;i<$length1;i++));do
                echo "
                auto ${BNIC1_SLAVE[i]}
                iface ${BNIC1_SLAVE[i]} inet manual
                bond-master $BNIC1"  >>$FILENAME
        if [ $2 -eq 1 ]; then
                if [ $i -eq 0 ]; then
                echo "
                bond-primary ${BNIC1_SLAVE[i]}" >>$FILENAME
                fi
        fi
        done

        echo "
                auto    $BNIC1
                iface   $BNIC1 inet static
                address $BNIC1_ADDR
                netmask $BNIC_MASK
                gateway $BNIC_GW
        " >>$FILENAME

  for((i=0;i<$length1;i++));do
        echo "
                up ifenslave $BNIC1 ${BNIC1_SLAVE[i]}
                down ifenslave $BNIC1 -d ${BNIC1_SLAVE[i]}
        "  >>$FILENAME
        done

  echo "
                bond-mode $2
                bond-miimon 100
                bond-slaves none
  "   >>$FILENAME
        echo "
                alias $BNIC1 bonding
                options $BNIC1 mode=$2 miimon=100 max_bonds=2 
             " >>$FILENAME2

fi

#write the third bond2 to interfaces
if [ $length2 -ne 0 ]; then
        for((i=0;i<$length2;i++));do
                echo "
                auto ${BNIC2_SLAVE[i]}
                iface ${BNIC2_SLAVE[i]} inet manual
                bond-master $BNIC2"  >>$FILENAME
        if [ $3 -eq 1 ]; then
                if [ $i -eq 0 ]; then
                echo "
                bond-primary ${BNIC2_SLAVE[i]}" >>$FILENAME
                fi
        fi
        done

        echo "
                auto    $BNIC2
                iface   $BNIC2 inet static
                address $BNIC2_ADDR
                netmask $BNIC_MASK
                gateway $BNIC_GW
        " >>$FILENAME

  for((i=0;i<$length2;i++));do
        echo "
                up ifenslave $BNIC2 ${BNIC2_SLAVE[i]}
                down ifenslave $BNIC2 -d ${BNIC2_SLAVE[i]}
        "  >>$FILENAME
        done

  echo "
                bond-mode $3
                bond-miimon 100
                bond-slaves none
  "   >>$FILENAME
        echo "
                alias $BNIC2 bonding
                options $BNIC2 mode=$3 miimon=100 max_bonds=2 
             " >>$FILENAME2

fi



sudo mv $FILENAME /etc/network/
sudo mv $FILENAME2 /etc/
exit 0
